#include <irrlicht.h>
#include "world.h"
#include "keyboard_events.h"

using namespace irr;

#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif

const u8 DELTA_TIME_COUNT = 10;

int main()
{
	IrrlichtDevice *device =
    createDevice(EDT_OPENGL, dimension2d<unsigned int>(1000, 600), 16,
        false, true, true, 0);
	
	KeyboardEventsReceiver receiver;
	device->setEventReceiver(&receiver);
	
	device->setWindowCaption(L"Airplanes concept");

	if (device == 0) return 1; // could not create selected driver.

	video::IVideoDriver* driver = device->getVideoDriver();
	scene::ISceneManager* smgr = device->getSceneManager();
	gui::IGUIEnvironment* env = device->getGUIEnvironment();

	gui::IGUISkin* skin = env->getSkin();
	gui::IGUIFont* font = env->getFont("./Data/Fonts/fontlucida.png");
	if (font) skin->setFont(font);
	skin->setFont(env->getBuiltInFont(), gui::EGDF_TOOLTIP);

	//Create World

	World world(device);
	world.time = 0;

	u32 prevTime = device->getTimer()->getTime();
	u8 test = 0;
	while(device->run())
	{
		u32 curTime = device->getTimer()->getTime();
		if (device->isWindowActive() && (curTime-prevTime) > 15)
		{

			world.deltaTime = curTime-prevTime;
			world.time = curTime;

			world.ProcessKeyboardEvents(&receiver);

			//Airplanes moved, bullet also moved, fires animate here
			world.RestructSplines();

			driver->beginScene(true, false, video::SColor(255, 153, 255, 255));
			
			//smgr->drawAll();
			world.DrawWorld();
			//device->getGUIEnvironment()->drawAll();

			driver->endScene();
			prevTime = curTime;
		}
		else device->yield();
	}

	device->drop();
	return 0;
}